
define(function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: '#WfTaskpoolList',
      data: function () {
        var _this = this;
        return {
          formdata: {},
          urls: {
            dataUrl: backend.workflowService + '/api/taskpool/',
            show: backend.workflowService + '/api/taskpool/',
            createSaveUrl: backend.workflowService + '/api/taskpool/',
            updateSaveUrl: backend.workflowService + '/api/taskpool/update/',
            batchdelete: backend.workflowService + '/api/taskpool/delete/',
            DutyUrl: backend.workflowService + '/api/org/dutys/',
            index: backend.workflowService + '/api/taskpool/config/'
          },
          tableColumns: [
            { label: '项目池编号', prop: 'poolId', resizable: true},
            { label: '项目池名称', prop: 'poolName', resizable: true},
            { label: '描述', prop: 'poolDesc', resizable: true},
            { label: '机构ID', prop: 'orgId', resizable: true}
          ],
          DutyTableColumns: [
            { label: '岗位码', prop: 'dutyId', resizable: true},
            { label: '岗位名称', prop: 'dutyName', resizable: true}
          ],
          NewFields: [
            {
              columnCount: 2,
              fields: [
                { field: 'poolId', label: '项目池编号', type: 'input', rules: [{ required: true, message: '必填项', trigger: 'blur' }] },
                { field: 'poolName', label: '项目池名称', type: 'input', rules: [{ required: true, message: '必填项', trigger: 'blur' }] },
                { field: 'systemId', label: '系统ID', type: 'input', hidden: true }
              ]
            },
            {
              columnCount: 1,
              fields: [
                { field: 'poolDesc', label: '描述', type: 'textarea', rows: 1, rules: [{ max: 100, message: '最大长度不超过100个字符', trigger: 'blur' }] }
              ]
            }
          ],
          updateFields: [
            {
              columnCount: 2,
              fields: [
                { field: 'poolId', label: '项目池编号', type: 'input', disabled: true, rules: [{ required: true, message: '必填项', trigger: 'blur' }] },
                { field: 'poolName', label: '项目池名称', type: 'input', disabled: true, rules: [{ required: true, message: '必填项', trigger: 'blur' }] },
                { field: 'systemId', label: '系统ID', type: 'input', hidden: true }
              ]
            },
            {
              columnCount: 1,
              fields: [
                { field: 'poolDesc', label: '描述', type: 'textarea', rows: 1, rules: [{ max: 100, message: '最大长度不超过100个字符', trigger: 'blur' }] }
              ]
            }
          ],
          dutyFields: [
            { placeholder: '岗位码', field: 'dutyId', type: 'input' },
            { placeholder: '岗位名称', field: 'dutyName', type: 'input' }
          ],
          dutyButtons: [
            {
              label: '搜索',
              op: 'submit',
              type: 'primary',
              show: true,
              click: function (model, valid) {
                if (valid) {
                  var params = {};
                  params.dutyId = model.dutyId;
                  if (model.dutyName != '') {
                    params.dutyName = '%' + model.dutyName + '%';
                  }
                  var param = {
                    condition: JSON.stringify(params)
                  };
                  _this.$refs.dutyTable.remoteData(param);
                }
              }
            },
            { label: '重置', op: 'reset', show: this.resetButton }
          ],
          mainGrid: {
            query: {
              poolId: '',
              poolName: ''
            }
          },
          dataParams: {
            condition: JSON.stringify({})
          },
          poolId: '',
          checkbox: true,
          DutyParams: { condition: JSON.stringify({ systemId: 'cmis' }) },
          height: yufp.custom.viewSize().height - 100,
          dialogFormVisible: false,
          modifydialogFormVisible: false,
          dialogVisibleDuty: false,
          dialogStatus: '',
          formDisabled: false,
          textMap: {
            update: '修改',
            creat: '新增'
          },
          DutyTitle: '关联岗位',
          checkDutyList: [],
          checkList: '',
          checkDutyForm: {},
          dutyMembers: []
        };
      },
      methods: {
        openDutyFn: function () { // 打开关联岗位页面
          var _this = this;
          if (_this.$refs.WfTaskPoolList.selections.length !== 1) {
            _this.$message({ duration: 4000, message: '请选择一条记录！', type: 'warning' });
            return false;
          }
          var row = _this.$refs.WfTaskPoolList.selections[0];
          _this.checkDutyLists(row.poolId);
          _this.dialogVisibleDuty = true;
        },
        checkDutyLists: function (poolId) {
          var _this = this;
          _this.checkDutyList = [];
          _this.$nextTick(function () {
            var param = {
              condition: JSON.stringify({ poolId: poolId })
            };
            yufp.service.request({
              method: 'GET',
              url: _this.urls.DutyUrl,
              data: _this.DutyParams,
              callback: function (code, message, response) { // 所有岗位
                if (response.code == 0) {
                  var allDuty = response.data;
                  _this.dutyMembers = response.data;
                  yufp.service.request({
                    method: 'GET',
                    url: _this.urls.index,
                    data: param,
                    callback: function (code, message, response) { // 已配置岗位
                      if (response.code != 0) {
                        _this.$message({ duration: 4000, message: 'error:' + response.message, type: 'error' });
                      } else {
                        var users = response.data;
                        for (var i = 0; i < users.length; i++) {
                          for (var k = 0; k < allDuty.length; k++) {
                            if (users[i].code == allDuty[k].dutyId) {
                              _this.checkDutyList.push(allDuty[k]);
                              _this.$refs.dutyTable.toggleRowSelection(_this.$refs.dutyTable.tableData[k], true);
                            }
                          }
                        }
                      }
                    }
                  });
                } else {
                  _this.$message({ duration: 4000, message: 'error:' + response.message, type: 'error' });
                }
              }
            });
          });
        },
        dutySelect: function (selection) { // 选择项改变事件
          this.checkDutyList = selection;
        },
        TagClose: function (tag) { // tag标签关闭事件-在tag标签里加上 closable @close="TagClose(tag)"
          this.checkDutyList.splice(this.checkDutyList.indexOf(tag), 1);
          var tableData = this.$refs.dutyTable.tableData;
          if (tableData != null) {
            for (var i = 0; i < tableData.length; i++) {
              if (tableData[i].dutyId == tag.dutyId) {
                this.$refs.dutyTable.toggleRowSelection(tableData[i], false);
              }
            }
          }
        },
        rowClick: function (row) { // 表格单击事件
          this.$refs.dutyTable.toggleRowSelection(row);
        },
        // 项目池关联岗位
        saveDuty: function () {
          var _this = this;
          if (_this.$refs.dutyTable.selection.length < 1) {
            _this.$message({ duration: 4000, message: '请至少选择一条数据!', type: 'warning' });
            return false;
          }
          var list = _this.$refs.dutyTable.selection;
          var commit = [];
          var taskpool = _this.$refs.WfTaskPoolList.selections[0];
          for (var i = 0; i < list.length; i++) {
            var data = {
              poolId: taskpool.poolId,
              code: list[i].dutyId,
              poolType: 'G'
            };
            commit.push(data);
          }
          yufp.service.request({
            method: 'POST',
            url: backend.workflowService + '/api/taskpool/config/batchdelete/' + taskpool.poolId,
            callback: function (code, message, response) {
              if (response.code == 0) {
                for (var i = 0; i < commit.length; i++) {
                  var param = commit[i];
                  yufp.service.request({
                    method: 'POST',
                    url: _this.urls.index,
                    data: param,
                    callback: function (code, message, response) {
                      if (response.data != null) {
                        _this.$message({ duration: 4000, message: '岗位关联完成', type: 'success' });
                      } else {
                        _this.$message({ duration: 4000, message: '岗位' + param.code + '关联失败', type: 'error' });
                      }
                    }
                  });
                }
                _this.checkDutyLists(taskpool.poolId);
              } else {
                _this.$message({ duration: 4000, message: 'error:' + response.message, type: 'error' });
              }
            }
          });
        },
        beforeCloseDuty: function () {
          var _this = this;
          _this.dialogVisibleDuty = false;
          _this.checkDutyList = [];
          _this.$refs.WfTaskPoolList.remoteData();
          _this.$refs.dutyTable.clearSelection();
        },
        openCreateFn: function () { // 打开新增页面
          this.opType('creat', false);
          this.$nextTick(function () {
            this.$refs.WfTaskPoolForm.resetFields();
          });
        },
        openEditFn: function () { // 打开修改页面
          if (this.$refs.WfTaskPoolList.selections.length !== 1) {
            this.$message({ duration: 4000, message: '请选择一条数据!', type: 'warning' });
            return false;
          }
          var row = this.$refs.WfTaskPoolList.selections[0];
          this.poolId = row.poolId;
          this.opType('update', false);
          this.$nextTick(function () {
            this.$refs.reform.resetFields();
            yufp.extend(this.$refs.reform.formModel, row);
          });
        },
        opType: function (type, disabled) { // 切换弹框状态
          if (type == 'creat') {
            this.dialogFormVisible = true;
          } else if (type == 'update') {
            this.modifydialogFormVisible = true;
          }
          this.dialogStatus = type;
          this.formDisabled = disabled;
        },
        saveCreateFn: function () { // 新增保存
          var _this = this;
          var myform = _this.$refs.WfTaskPoolForm;
          myform.formModel.systemId = 'cmis';
          myform.formModel.orgId = yufp.session.org.code;
          myform.validate(function (valid) {
            if (valid) {
              // 特殊字符校验
              var reg = new RegExp('[`~!@#%$^&*()=|{}\':;\',\\[\\].<>/?~！@#￥……&*（）|{}【】‘；：”“\'。，、？]');
              if (reg.test(myform.formModel.poolId) || reg.test(myform.formModel.poolName)) {
                _this.$message({ message: '包含特殊字符', type: 'warning' });
                return;
              }
              var comitData = {};
              yufp.extend(comitData, myform.formModel);
              yufp.service.request({// 检测项目池编号唯一性
                url: _this.urls.show + myform.formModel.poolId,
                method: 'GET',
                callback: function (code, message, response) {
                  if (response.code == 0) {
                    if (response.data == null) {
                      yufp.service.request({
                        url: _this.urls.createSaveUrl,
                        data: comitData,
                        method: 'POST',
                        callback: function (code, message, response) {
                          if (response.code == 0) {
                            _this.dialogFormVisible = false;
                            _this.$message({ duration: 4000, message: '新增成功', type: 'success' });
                            _this.$refs.WfTaskPoolList.remoteData();
                          } else {
                            _this.$message({ duration: 4000, message: '新增异常:' + response.message, type: 'error' });
                          }
                        }
                      });
                    } else {
                      var Message = '项目池编号不能重复，请重新输入！';
                      _this.$message({ duration: 4000, message: Message, type: 'warning' });
                    }
                  } else {
                    _this.$message({ duration: 4000, message: '异常:' + response.message, type: 'error' });
                  }
                }
              });
            } else {
              _this.$message({ duration: 4000, message: '请检查输入项是否合法', type: 'warning' });
              return false;
            }
          });
        },
        saveEditFn: function () { // 修改保存
          var _this = this;
          var myform = _this.$refs.reform;
          myform.formModel.systemId = 'cmis';
          myform.formModel.orgId = yufp.session.org.code;
          myform.validate(function (valid) {
            if (valid) {
              var comitData = {};
              yufp.extend(comitData, myform.formModel);
              yufp.service.request({
                url: _this.urls.show + myform.formModel.poolId,
                method: 'GET',
                callback: function (code, message, response) {
                  if (response.code == 0) {
                    if (response.data != null) {
                      yufp.service.request({
                        url: _this.urls.updateSaveUrl,
                        data: comitData,
                        method: 'POST',
                        callback: function (code, message, response) {
                          if (response.code == 0) {
                            _this.modifydialogFormVisible = false;
                            _this.$message({ duration: 4000, message: '修改成功', type: 'success' });
                            _this.$refs.WfTaskPoolList.remoteData();
                          } else {
                            _this.$message({ duration: 4000, message: '修改异常:' + response.message, type: 'error' });
                          }
                        }
                      });
                    } else {
                      _this.$message({ duration: 4000, message: '该项目池编号的数据不存在!', type: 'warning' });
                    }
                  } else {
                    _this.$message({ duration: 4000, message: 'error:' + response.message, type: 'error' });
                  }
                }
              });
            } else {
              _this.$message({ duration: 4000, message: '请检查输入项是否合法', type: 'warning' });
              return false;
            }
          });
        },
        // 删除项目池信息
        deleteFn: function () { // 删除
          if (this.$refs.WfTaskPoolList.selections.length > 0) {
            var row = this.$refs.WfTaskPoolList.selections[0];
            var _this = this;
            this.$confirm('您确定需要删除该条记录吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: _this.urls.batchdelete + row.poolId,
                callback: function (code, message, response) {
                  if (response.code == 0) {
                    _this.$message({ duration: 4000, message: '删除成功', type: 'success' });
                    _this.$refs.WfTaskPoolList.remoteData();
                  } else {
                    _this.$message({ duration: 4000, message: '删除异常:' + response.message, type: 'error' });
                  }
                }
              });
            });
          } else {
            this.$message({ duration: 4000, message: '请先选择要删除的数据', type: 'warning' });
            return false;
          }
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});